﻿using System;
using System.Web;
using System.Web.Mvc;

namespace Nop.Web.Framework
{
    public static class UrlHelperExtensions
    {
        public static string LogOn(this UrlHelper urlHelper, string returnUrl)
        {
            if (!string.IsNullOrEmpty(returnUrl))
                return urlHelper.Action("Login", "Customer", new { ReturnUrl = returnUrl });
            return urlHelper.Action("Login", "Customer");
        }

        public static string LogOff(this UrlHelper urlHelper, string returnUrl)
        {
            if (!string.IsNullOrEmpty(returnUrl))
                return urlHelper.Action("Logout", "Customer", new { ReturnUrl = returnUrl });
            return urlHelper.Action("Logout", "Customer");
        }
        /// <summary>
        /// Define short cut method to get current application root url.
        /// </summary>
        /// <param name="urlHelper"></param>
        /// <returns></returns>
        public static string GetBaseUrl(this UrlHelper urlHelper, string path = "", string rootUrl = "")
        {
            var baseUrl = !string.IsNullOrEmpty(rootUrl) ? rootUrl :
               string.Format("{0}://{1}{2}", urlHelper.RequestContext.HttpContext.Request.Url.Scheme, urlHelper.RequestContext.HttpContext.Request.Url.Authority, HttpRuntime.AppDomainAppVirtualPath);

            if (!string.IsNullOrEmpty(path))
            {
                if (path.StartsWith("/"))
                {
                    path = path.Substring(1);
                }
                return System.IO.Path.Combine(baseUrl, path);
            }
            return baseUrl;
        }

        /// <summary>
        /// get current category Id
        /// </summary>
        /// <param name="urlHelper"></param>
        /// <returns></returns>
        public static int GetCurrentCategoryId(this UrlHelper urlHelper)
        {
            //current category ID
            int currentCategoryId = 0;
            if (urlHelper.RequestContext.RouteData.Values["controller"].ToString().Equals("catalog", StringComparison.InvariantCultureIgnoreCase) &&
                urlHelper.RequestContext.RouteData.Values["action"].ToString().Equals("category", StringComparison.InvariantCultureIgnoreCase))
            {
                currentCategoryId = Convert.ToInt32(urlHelper.RequestContext.RouteData.Values["categoryId"].ToString());
            }
            return currentCategoryId;
        }
        /// <summary>
        /// current manufacturer ID
        /// </summary>
        /// <param name="urlHelper"></param>
        /// <returns></returns>
        public static int GetCurrentManufacturerId(this UrlHelper urlHelper)
        {
            //current manufacturer ID
            int currentManufacturerId = 0;
            if (urlHelper.RequestContext.RouteData.Values["controller"].ToString().Equals("catalog", StringComparison.InvariantCultureIgnoreCase) &&
                urlHelper.RequestContext.RouteData.Values["action"].ToString().Equals("manufacturer", StringComparison.InvariantCultureIgnoreCase))
            {
                currentManufacturerId = Convert.ToInt32(urlHelper.RequestContext.RouteData.Values["manufacturerId"].ToString());
            }
            return currentManufacturerId;
        }
        /// <summary>
        /// current product ID
        /// </summary>
        /// <param name="urlHelper"></param>
        /// <returns></returns>
        public static int GetCurrentProductId(this UrlHelper urlHelper)
        {
            //current product ID
            int currentProductId = 0;
            if (urlHelper.RequestContext.RouteData.Values["controller"].ToString().Equals("product", StringComparison.InvariantCultureIgnoreCase) &&
                urlHelper.RequestContext.RouteData.Values["action"].ToString().Equals("productdetails", StringComparison.InvariantCultureIgnoreCase))
            {
                currentProductId = Convert.ToInt32(urlHelper.RequestContext.RouteData.Values["productId"].ToString());
            }
            return currentProductId;
        }

        /// <summary>
        /// show all categories pop menu, if current page is category, manufacturer, product page
        /// </summary>
        /// <param name="urlHelper"></param>
        /// <returns></returns>
        public static bool ShowAllCategoryControl(this UrlHelper urlHelper)
        {
            var routeValues = urlHelper.RequestContext.RouteData.Values;
            if (routeValues["categoryId"] != null
                || routeValues["productId"] != null
                || routeValues["manufacturerId"] != null)
            {
                return true;
            }
            return false;
        }

    }
}
